home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 December / PCWDEC07.iso / Software / Freeware / DownthemAll 1.0b1 / components / privacycontrol.js
Encoding:
Text File  |  2006-04-09  |  6.8 KB  |  254 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the downTHEMall Privacy component.
  15.  *
  16.  * The Initial Developer of the Original Code is Nils Maier
  17.  * Portions created by the Initial Developer are Copyright (C) 2006
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Nils Maier <MaierMan@web.de>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37.  
  38. const CONTRACTID = "@downthemall.net/privacycontrol;1";
  39. const CID = Components.ID("{db7a8d60-a4c7-11da-a746-0800200c9a66}");
  40.  
  41. // we support these interfaces.
  42. const IIDs = [
  43.   Components.interfaces.nsISupports,
  44.   Components.interfaces.nsIObserver,
  45.   CID
  46. ];
  47.  
  48. // helper : check if interface is supported
  49. function testIID(aIID)
  50. {
  51.   for (var i = 0; i < IIDs.length; ++i) {
  52.     if (aIID.equals(IIDs[i]))
  53.       return true;
  54.   }
  55.   return false;
  56. }
  57.  
  58.  
  59. // c'tor
  60. function privacycontrol() {
  61.   this.initialize();
  62. }
  63.  
  64. privacycontrol.prototype = {
  65.  
  66.   _logService: null,
  67.  
  68.   QueryInterface: function(aIID) {
  69.  
  70.     if (testIID(aIID))
  71.       return this;
  72.  
  73.     throw Components.results.NS_ERROR_NO_INTERFACE;
  74.   },
  75.  
  76.   initialize: function() {
  77.  
  78.     this._logService = Components.classes["@mozilla.org/consoleservice;1"]
  79.       .getService(Components.interfaces.nsIConsoleService);
  80.  
  81.     // install required observers, so that we may process on shutdown
  82.     const os = Components.classes['@mozilla.org/observer-service;1']
  83.       .getService(Components.interfaces.nsIObserverService);
  84.     os.addObserver(this, 'profile-change-teardown', false);
  85.     os.addObserver(this, 'xpcom-shutdown', false);
  86.   },
  87.  
  88.   log : function(aMsg) {
  89.     if (this._logService instanceof Components.interfaces.nsIConsoleService) {
  90.       this._logService.logStringMessage('dta privacyontrol: ' + aMsg);
  91.     }
  92.   },
  93.  
  94.   dispose: function () {
  95.  
  96.     // always remove observers ;)
  97.     const os = Components.classes['@mozilla.org/observer-service;1']
  98.       .getService(Components.interfaces.nsIObserverService);
  99.     os.removeObserver(this, 'profile-change-teardown');
  100.     os.removeObserver(this, 'xpcom-shutdown');
  101.   },
  102.  
  103.   observe: function(subject, topic, data) {
  104.  
  105.     switch (topic) {
  106.  
  107.     case 'xpcom-shutdown':
  108.       this.dispose();
  109.     break;
  110.  
  111.     case 'profile-change-teardown':
  112.       this.onShutdown();
  113.     break;
  114.  
  115.     case 'sanitize':
  116.       this.sanitize();
  117.     break;
  118.  
  119.     case 'clean':
  120.       this.clean();
  121.     break;
  122.  
  123.     }
  124.   },
  125.  
  126.   clean: function() {
  127.  
  128.     this.log('clean()');
  129.     const prefs = Components.classes["@mozilla.org/preferences-service;1"]
  130.       .getService(Components.interfaces.nsIPrefService)
  131.       .getBranch('extensions.dta.');
  132.  
  133.     // add more if needed
  134.     const toClear = Array('directory', 'dropdown.directory-current', 'dropdown.directory-history');
  135.  
  136.     for (var i = 0; i < toClear.length; ++i) {
  137.       try {
  138.         prefs.clearUserPref(toClear[i]);
  139.       } catch (ex) {}
  140.     }
  141.   },
  142.  
  143.   sanitize: function() {
  144.  
  145.     this.log('sanitize()');
  146.     const prefs = Components.classes["@mozilla.org/preferences-service;1"]
  147.       .getService(Components.interfaces.nsIPrefService)
  148.       .getBranch('privacy.');
  149.  
  150.     // in case UI should be used the cleaning will be processed there.
  151.     // Futhermore we have to ensure user wants us to sanitize.
  152.     if (!prefs.getBoolPref('sanitize.promptOnSanitize') && prefs.getBoolPref('item.extensions-dta'))
  153.       this.clean(prefs);
  154.  
  155.   },
  156.  
  157.   onShutdown : function()
  158.   {
  159.     this.log('onShutdown()');
  160.     const prefs = Components.classes["@mozilla.org/preferences-service;1"]
  161.       .getService(Components.interfaces.nsIPrefService)
  162.       .getBranch('privacy.');
  163.  
  164.     // has user pref'ed to sanitize on shutdown?
  165.     if (prefs.getBoolPref('sanitize.sanitizeOnShutdown'))
  166.       this.sanitize();
  167.  
  168.   }
  169.  
  170. }
  171.  
  172. // little factory.
  173. const factory = {
  174.   instance: null,
  175.  
  176.   QueryInterace : function(aIID) {
  177.     if (aIID.equals(Components.interfaces.nsIFactory) || aIID.equals(Components.interfaces.nsIFactory))
  178.       return this;
  179.  
  180.     return Components.results.NS_ERROR_NO_INTERFACE;
  181.   },
  182.  
  183.   createInstance: function(aOuter, aIID)
  184.   {
  185.     if (aOuter != null)
  186.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  187.  
  188.     // alright. we want a singleton!
  189.     if (testIID(aIID))
  190.       return this.instance ? this.instance : (this.instance = new privacycontrol());
  191.  
  192.     throw Components.results.NS_ERROR_INVALID_ARG;
  193.  
  194.   }
  195. };
  196.  
  197. const module = {
  198.  
  199.   regged: false,
  200.  
  201.   registerSelf: function(mgr, spec, location, type) {
  202.  
  203.     // reg only once.
  204.     if (this.regged)
  205.       return;
  206.     this.regged = true;
  207.  
  208.     mgr.QueryInterface(Components.interfaces.nsIComponentRegistrar)
  209.       .registerFactoryLocation(
  210.         CID,
  211.         'downTHEMall Privacy Control',
  212.         CONTRACTID,
  213.         spec,
  214.         location,
  215.         type
  216.     );
  217.  
  218.     // this will create the initial instance, which will install the observers
  219.     Components.classes['@mozilla.org/categorymanager;1']
  220.       .getService(Components.interfaces.nsICategoryManager)
  221.       .addCategoryEntry('app-startup', CONTRACTID, CONTRACTID, true, true, null);
  222.  
  223.   },
  224.  
  225.   unregisterSelf: function(mgr, spec, location) {
  226.  
  227.     if (!this.regged)
  228.       return;
  229.  
  230.     mgr.QueryInterface(Components.interfaces.nsIComponentRegistrar)
  231.       .unregisterFactoryLocation(CID, spec);
  232.  
  233.     Components.classes['@mozilla.org/categorymanager;1']
  234.       .getService(Components.interfaces.nsICategoryManager)
  235.       .deleteCategoryEntry('app-startup', CONTRACTID, true);
  236.  
  237.   },
  238.  
  239.   getClassObject: function(mgr, cid, iid)
  240.   {
  241.     if (!cid.equals(CID))
  242.       throw Components.results.NS_NO_INTERFACE;
  243.  
  244.     if (!iid.equals(Components.interfaces.nsIFactory))
  245.       throw Components.results.NS_NOT_IMPLEMENTED;
  246.  
  247.     return factory;
  248.   }
  249. };
  250.  
  251. function NSGetModule(mgr, spec) {
  252.   return module;
  253. }
  254.